package com.itheima.app.mapper;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.service.db.AnnouncementService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.AnnouncementVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessageManager {

    @Reference
    private CommentService commentService;

    @Reference
    private UserInfoService userInfoService;

    @Reference
    private AnnouncementService AnnouncementService;

    //根据评论类型分页查询
    public PageBeanVo findUserComment(Integer commentType, Integer pageNum, Integer pageSize) {
        //1. 获取登录用户id
        Long userId = UserHolder.getUser().getId();

        //2. 查询到了评论信息
        PageBeanVo pageBeanVo = commentService.findComment(commentType, userId, pageNum, pageSize);

        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        List<UserCommentVo> userCommentVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                UserCommentVo userCommentVo = new UserCommentVo();

                //设置评论相关的
                userCommentVo.setId(comment.getId().toHexString());//评论id
                userCommentVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(comment.getCreated())));//评论时间

                //设置用户相关的
                UserInfo userInfo = userInfoService.findById(comment.getUserId());
                userCommentVo.setAvatar(userInfo.getAvatar());//设置评论者头像
                userCommentVo.setNickname(userInfo.getNickname());//设置评论者昵称

                userCommentVoList.add(userCommentVo);
            }
        }
        pageBeanVo.setItems(userCommentVoList);
        return pageBeanVo;
    }

    //查询公告列表
    public PageBeanVo findAnnouncements(Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo = AnnouncementService.findAnnouncements(pageNum,pageSize);
        List<Announcement> AnnouncementList =( List<Announcement>) pageBeanVo.getItems();
        List<AnnouncementVo> AnnouncementVoList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(AnnouncementList)){
            for (Announcement announcement : AnnouncementList) {
                AnnouncementVo announcementVo = new AnnouncementVo();
                BeanUtils.copyProperties(announcement,announcementVo);//复制announcement
                announcementVo.setId(announcement.getId()+"");//设置id
                announcementVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(announcement.getCreated()));
                AnnouncementVoList.add(announcementVo);
            }
        }
        pageBeanVo.setItems(AnnouncementVoList);
        return pageBeanVo;
    }
}